//
// Created by fanghr on 18-3-26.
//

#include <iostream>
#include "../../acclsen/include/acclsen_driver2cpp.h"
#include "binder_cpp.h"
#include "../include/service_api.h"

acclsen_driver2cpp *sensor;

int handler(binder_state *bs, binder_transaction_data *txn, binder_io *msg, binder_io *reply) {
    uint16_t *s;
    size_t size;
    uint32_t handle;
    uint32_t strict_policy;
    int i;

    strict_policy = bio_get_uint32(msg);
    switch (txn->code) {
        case ACCLSEN_GET_TIME:
            auto time = sensor->get_time();
            bio_put_data(reply, (const uint16_t *) &time, sizeof(time));
            return 0;
        case ACCLSEN_GET_ACC:
            auto acc = sensor->get_acc();
            bio_put_data(reply, (const uint16_t *) &acc, sizeof(acc));
            return 0;
        case ACCLSEN_GET_ANG:
            auto ang = sensor->get_ang();
            bio_put_data(reply, (const uint16_t *) &ang, sizeof(ang));
            return 0;
        case ACCLSEN_GET_ANGSP:
            auto angsp = sensor->get_angsp();
            bio_put_data(reply, (const uint16_t *) &angsp, sizeof(angsp));
            return 0;
        case ACCLSEN_GET_MAGF:
            auto magf = sensor->get_magf();
            bio_put_data(reply, (const uint16_t *) &magf, sizeof(magf));
            return 0;
        case ACCLSEN_GET_GRSP:
            auto grsp = sensor->get_grsp();
            bio_put_data(reply, (const uint16_t *) &grsp, sizeof(grsp));
            return 0;
        case ACCLSEN_GET_LATLON:
            auto latlon = sensor->get_latlon();
            bio_put_data(reply, (const uint16_t *) &latlon, sizeof(latlon));
            return 0;
        case ACCLSEN_GET_PREHEI:
            auto prehei = sensor->get_prehei();
            bio_put_data(reply, (const uint16_t *) &prehei, sizeof(prehei));
            return 0;
        case ACCLSEN_GET_PORT:
            auto port = sensor->get_port();
            bio_put_data(reply, (const uint16_t *) &port, sizeof(port));
            return 0;
        case ACCLSEN_GET_FOEL:
            auto foel = sensor->get_foel();
            bio_put_data(reply, (const uint16_t *) &foel, sizeof(foel));
            return 0;
        default:
            return -1;
    }
}

int main() {
    sensor = new acclsen_driver2cpp();
    sensor->run();
    binder_cpp::binder binder(128 * 1024);
    binder_cpp::server server(binder);

    if (server.publish(BINDER_SERVICE_MANAGER, const_cast<char *>("acclsen"), nullptr)) {
        std::cout << "fail to add service";
        return -1;
    }
    server.loop(handler);
    return 0;
}